package com.angel.architecture.runner;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.angel.common.interfaces.Executable;

/**
 *	It allows run an application process with a spring context.
 *	</br>
 *	For example to run a data generator process in a main application, you only must do following code:
 *	</br>
 *	<pre>
 *	String[] yourApplicationContextFiles = new String()[]{"appContextExample1.xml","appContextExample2.xml"};(1)
 *	ApplicationContextSpring applicationContextSpring = ApplicationContextSpring.createInstance(yourApplicationContextFiles).prepareApplicationContext();(2)
 *	ApplicationProcessRunner applicationProcessRunner = applicationContextSpring.getApplicationProcessRunner();(3)
 *	applicationProcessRunner.addExecutable(new YourDataGeneratorExecutable());(4)
 *	applicationProcessRunner.run();(5)
 *	</pre>
 *
 *	Class {@link Executable} must be implemented by your batch process to set it in {@link ApplicationProcessRunner}.</br>
 *
 *	In this example code there are 5 code lines to execute a batch process.</br>
 *	(1): Define your spring context files in your class path.</br>
 *	(2): Create a spring application context with default and obligatory application context and your context files.</br>
 *	(3): Get application process runner to run your batch process (in this example YourDataGeneratorExecutable()).</br>
 *	(4): Set executable process to application process runner.</br>
 *	(5): Run batch process.</br>
 * </br>
 * @author William
 * @since 24/August/2009
 *
 */
public class ApplicationProcessRunner extends HibernateDaoSupport{

	private List<Executable> executables;

	public ApplicationProcessRunner(){
		super();
		this.setExecutables(new ArrayList<Executable>());
	}

	public ApplicationProcessRunner(Collection<Executable> executables){
		this();
		this.getExecutables().addAll(executables);
	}

	/**
	 * @param executable the executable to set
	 */
	public void addExecutable(Executable executable) {
		this.executables.add(executable);
	}

	public void run(){
		for(Executable e: this.getExecutables()){
			e.execute();
		}
	}

	public void run(Executable executable){
		executable.execute();
	}

	public List<Executable> getExecutables() {
		return executables;
	}

	public void setExecutables(List<Executable> executables) {
		this.executables = executables;
	}
}

